from django.shortcuts import render
from django.http import HttpResponse
from .models import Question,QuestionType,Choice,Answer,PaperKu
from random import sample
from .oo import ProducePaper

# Create your views here.

def index(request):#试卷选择
    #print(request.META.get("HTTP_API_AUTH"))
    print(request.method)
    #print(vars(request))
    print(request.META.get('HTTP_USER_AGENT'))
    print(request.META.get('REMOTE_ADDR'))
    
    context = {}
    return render(request,'paper/index.html',context)

def index_yunsuan(request):#生成试卷必要运算
    info = dict(request.POST)
    paper_name = info['paper_name'][0]

    context_dict = {}
    info_dict = {}
    for question_type,question_info in info.items():
        if question_info[0] != '' and not \
           question_type.startswith('csrf') and \
           question_type !='paper_name':
                qu_type = QuestionType.objects.\
                          get(pk = question_info[0])
                question = sample(list(qu_type.question_set.all()),int(question_info[1]))
                question_id = [que.pk for que in question]
                #question_info[:].append(question)
                context_dict[qu_type.question_type] = question_info[:]
                
                context_dict[qu_type.question_type].append(question)

                info_dict[qu_type.question_type] = question_info[:]

                info_dict[qu_type.question_type].append(question_id)
                
                
                            

    context = context_dict
    
    print(context)
    print(info_dict)

    PaperKu(paper_name = paper_name,paper_info = info_dict).save()
    return render(request,'paper/index_yunsuan.html',{'context':context})
    

def dati(request):#数据渲染
    info = {'单选题':[10,1],
     '填空题':[5,2],
     '判断题':[5,1],
     '英汉互译题':[10,3]}

    context = ProducePaper(info).paper_info_obj
    
    print(context)
    #context = {}
    return render(request,'paper/paper.html',context)

def dati_tijiao_info(request):#接收考试完提交的信息，并运算
    
    context = request.POST
    print(context)
    return render(request,'paper/dati_tijiao_info.html',context)



def paper_ku_index(request):
    paper_ku = PaperKu.objects.all()

    context = {'paper_ku':paper_ku}
    return render(request,'paper/paper_ku_index.html',context)


def paper_show(request,paper_id):
    paper_info =eval(PaperKu.objects.get(pk = paper_id).paper_info)

    for key,value in paper_info.items():
        value[3]= [Question.objects.get(pk = pknow) \
                   for pknow in value[3]]
    

    context = {'context':paper_info}
    return render(request,'paper/index_yunsuan.html',context)












def info_jiekou(request):


	context = {}
	return render(request,'paper/info_jiekou.html',context)
	#return HttpResponse('提交成功')
	
def info_jiekou_yunsuan(request):

	'''<QueryDict: {'csrfmiddlewaretoken': 
	['Xu9GD5AqMDSwOub0pw7Ncjf3DfPB5yjcbcw2Dg
	o9MKAKc196rhH7lZfnWFTQ7R4z'], 
	'question_type': ['1'], 
	'question': ['所有M是P，所有S是M，
	因此不对的结论是（  ）'], 
	'choice': ['所有S是P', '所有S是Px', 
	'所有M是S', ''], 'answer': ['所有M是S']}>
	'''

	info_dict = dict(request.POST)
	type_id = info_dict['question_type'][0]
	if info_dict['question'][0] =='' or info_dict['answer'][0] == '':
	
		return HttpResponse('请检查是否输入问题和答案')
		
	
	question_type = QuestionType.objects.get(pk = type_id)
	
	question = question_type.question_set.create(
				question_text = info_dict['question'][0])
	
	question.answer_set.create(answer_text = info_dict['answer'][0])
	
	for choice in info_dict['choice']:
		if choice != '':
			question.choice_set.create(
				choice_text = choice)
	
	
	

	
	return HttpResponse('添加成功')
	


